<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="#">社团管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>社团类型列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-button type="success" icon="el-icon-check" id="bntAdd" @click="openAddClubType" >添加</el-button>
    <br/>
    <br/>
    <br/>
    <el-table
        :data="clubTypes"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID"
          width="400">
      </el-table-column>
      <el-table-column
          prop="name"
          label="社团类型"
          width="400">
      </el-table-column>
      <el-table-column
          fixed="right"
          width="300"
          label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="toClubTypeById(scope.row.id)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="open(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="社团类型" :visible.sync="dialogFormVisible" :modal-append-to-body="false">
      <el-form :model="form">
        <el-form-item label="类型名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false;">取 消</el-button>
        <el-button type="primary" @click="doClubInsert">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {clubTypeList, clubTypeInsert ,clubTypeById, clubTypeUpdate ,clubTypeDelete} from '@/api'
export default {
  name: "ClubTypeList",
  data() {
    return {
      clubTypes: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: '',
        id: 0,
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    clubTypeList().then(result=>{
      this.clubTypes = result.data;
    })
  },
  methods:{
    openAddClubType(){
      this.dialogFormVisible = true;
      this.form = {name: '', id: 0};
    },
    doClubInsert(){
       if (this.form.id ==0) {
         let clubType = {
           name: this.form.name,
         }
         this.dialogFormVisible = false;
         clubTypeInsert(clubType).then(() => {
           clubTypeList().then(result => {
             this.clubTypes = result.data;
           })
         });
       }else{
         let clubType = {
           id: this.form.id,
           name: this.form.name
         };
         this.form.id = 0;
         this.dialogFormVisible = false;
         clubTypeUpdate(clubType).then(()=>{
           clubTypeList().then(result => {
             this.clubTypes = result.data;
           })
         });
       }
     },

    toClubTypeById(id){
      this.dialogFormVisible = true;
       let clubType = {
         id: id,
       }
       this.form.id = id;
      clubTypeById(clubType).then((result)=>{
        this.form.name = result.data.name;
      })
    },
    open(id) {
      this.$confirm('此操作将永久删除该类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let clubType = {
          id: id,
        }
        clubTypeDelete(clubType).then(()=>{
          clubTypeList().then(result => {
            this.clubTypes = result.data;
          })
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

  }
}
</script>

<style scoped>
#bntAdd{
  float: right;
}
</style>
